﻿using Abp.Dependency;
using CommonServiceLocator;
using Prism.Regions.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Abp.Prism
{
    public class AbpServiceLocatorAdapter : ServiceLocatorImplBase
    {
        private readonly IIocManager _iocManager;
        public AbpServiceLocatorAdapter(IIocManager iocManager)
        {
            this._iocManager = iocManager;
        }
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return _iocManager.IocContainer.ResolveAll(serviceType).Cast<object>();            
        }

        protected override object DoGetInstance(Type serviceType, string key)
        {
            return key != null 
                ? _iocManager.IocContainer.Resolve(key, serviceType) 
                : _iocManager.IocContainer.Resolve(serviceType);
        }
    }
}
